package com.wing.res.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.request.ResWorkSiteUpdateForm;
import com.wing.res.model.request.ResWorkStationUpdateForm;
import com.wing.res.model.response.ResWorkSiteSearchVo;
import com.wing.res.model.response.ResWorkStationSearchVo;
import com.wing.res.service.ResWorkSiteService;
import com.wing.res.service.ResWorkStationService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 生产研发任务
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-19 15:36
 **/
@Slf4j
@Validated
@Api(value = "工站管理", tags = "#/res/setting/workSite")
@RestController("resWorkSiteController")
@RequestMapping("res/admin/setting/workSite")
public class ResWorkSiteController {

    @Resource
    private ResWorkSiteService resWorkSiteService;

    @ApiOperation(value = "查研工站列表", notes = "搜索-工站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ResWorkSiteSearchVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String name
            ,String code
            ,Boolean isDeleted
            ,String startCreateTime
            ,String endCreateTime
    ) {
        Page<ResWorkSiteSearchVo> pg = new Page<>(page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("code",code);
        map.put("isDeleted", isDeleted);
        if (StringUtils.isNotBlank(startCreateTime)) {
            map.put("startCreateTime", startCreateTime);
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            map.put("endCreateTime", endCreateTime);
        }
        Page<ResWorkSiteSearchVo> list =  resWorkSiteService.pageList4app(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    /**
     * 禁用
     * @param id
     * @return
     */
    @ApiOperation(value = "禁用", notes = "禁用")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/cancellation/{id}")
    public JsonResult cancellation(@PathVariable("id") String id) {
        int cancellation = resWorkSiteService.cancellation(id);
        if (cancellation > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }


    /**
     * 启用
     * @param id
     * @return
     */
    @ApiOperation(value = "启用", notes = "启用")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/recover/{id}")
    public JsonResult recover(@PathVariable("id") String id) {
        int cancellation = resWorkSiteService.recover(id);
        if (cancellation > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }

    /**
     * 修改工站信息
     * @param form
     * @return
     */
    @ApiOperation(value = "修改工站信息", notes = "工站-变更")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/updateWorkSiteBy")
    public JsonResult updateWorkSiteBy(@RequestBody ResWorkSiteUpdateForm form) {
        int result = resWorkSiteService.updateWorkSiteBy(form);
        if (result > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }


    /**
     * 添加工站信息
     * @param form
     * @return
     */
    @ApiOperation(value = "新增工站信息", notes = "工站-新增")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addWorkSiteBy")
    public JsonResult resWorkSiteService(@RequestBody ResWorkSiteUpdateForm form) {
        int result = resWorkSiteService.addWorkSiteBy(form);
        if (result > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }


}
